package com.tech.connect.zhaorencai;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.ksy.common.view.ProxyLayout;
import com.ksy.common.view.PullToRefreshLayout;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.FuWuZhanHttp;
import com.tech.connect.api.UserInfo;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.model.ItemFuWuZhan;
import com.tech.connect.zhaofuwu.CreateFuWuZhanActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RenCaiTeamFragment extends BaseFragment {

    private ImageView ivMy;
    private TextView tvMy;
    private RecyclerView myRecycler;
    private ImageView ivJoin;
    private TextView tvJoin;
    private RecyclerView joinRecycler;
    private BaseAdapter<ItemFuWuZhan, BaseHolder> myAdapter;
    private BaseAdapter<ItemFuWuZhan, BaseHolder> joinAdapter;
    private List<ItemFuWuZhan> allMyList = new ArrayList<>();
    private List<ItemFuWuZhan> allJoinList = new ArrayList<>();
    private ProxyLayout<NestedScrollView> proxyLayout;

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_rencai_team, null);
        return view;
    }

    @Override
    protected boolean isShowHeadBar() {
        return true;
    }

    @Override
    protected boolean isFullStateBar() {
        return true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getHeadBar().setLeftIcon(R.drawable.icon_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        }).setTitle("团队之家");
//        .setRightIcon(R.drawable.icon_search, new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        initView(view);
        loadMineListData();
        loadJoinListData();
    }


    private void initView(View view) {
        view.findViewById(R.id.llAdd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(CreateTeamActivity.class, 996);
            }
        });
        view.findViewById(R.id.llMy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int visibility = myRecycler.getVisibility();
                if (visibility == View.VISIBLE) {
                    myRecycler.setVisibility(View.GONE);
                    ivMy.setImageResource(R.drawable.icon_arrow_right);
                } else {
                    myRecycler.setVisibility(View.VISIBLE);
                    ivMy.setImageResource(R.drawable.icon_arrow_down);
                }
            }
        });
        view.findViewById(R.id.llJoin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int visibility = joinRecycler.getVisibility();
                if (visibility == View.VISIBLE) {
                    joinRecycler.setVisibility(View.GONE);
                    ivJoin.setImageResource(R.drawable.icon_arrow_right);
                } else {
                    joinRecycler.setVisibility(View.VISIBLE);
                    ivJoin.setImageResource(R.drawable.icon_arrow_down);
                }
            }
        });
        NestedScrollView scrollView = view.findViewById(R.id.scrollView);
        proxyLayout = new ProxyLayout<>(activity, scrollView);
        proxyLayout.setCanRefresh(true);
        proxyLayout.setDragListener(new PullToRefreshLayout.DragListener() {
            @Override
            public void drag2Upper(PullToRefreshLayout pullToRefreshLayout) {

            }

            @Override
            public void drag2Downer(PullToRefreshLayout pullToRefreshLayout) {
                loadMineListData();
                loadJoinListData();
            }
        });
        ivMy = view.findViewById(R.id.ivMy);
        tvMy = view.findViewById(R.id.tvMy);
        myRecycler = view.findViewById(R.id.myRecycler);
        ivJoin = view.findViewById(R.id.ivJoin);
        tvJoin = view.findViewById(R.id.tvJoin);
        joinRecycler = view.findViewById(R.id.joinRecycler);
        myRecycler.addItemDecoration(new HLineDivider());
        myRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        joinRecycler.addItemDecoration(new HLineDivider());
        joinRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
    }

    private void loadJoinListData() {
        Map<String, Object> map = new HashMap<>();
        FuWuZhanHttp.joinList(map, new BaseEntityOb<List<ItemFuWuZhan>>() {
            @Override
            public void onDataDeal(boolean isSuccess, List<ItemFuWuZhan> data, String msg) {
                initJoinAdapter(data);
                proxyLayout.dragFinish();
            }
        });
    }

    private void loadMineListData() {
        Map<String, Object> map = new HashMap<>();
        FuWuZhanHttp.mineList(map, new BaseEntityOb<List<ItemFuWuZhan>>() {
            @Override
            public void onDataDeal(boolean isSuccess, List<ItemFuWuZhan> data, String msg) {
                initMyAdapter(data);
                proxyLayout.dragFinish();
            }
        });
    }

    private void initMyAdapter(List<ItemFuWuZhan> data) {
        allMyList.clear();
        if (data != null) {
            allMyList.addAll(data);
        }
        if (allMyList.isEmpty()) {
            tvMy.setText("");
        } else {
            tvMy.setText(String.valueOf(allMyList.size()));
        }
        if (myAdapter == null) {
            myAdapter = new BaseAdapter<ItemFuWuZhan, BaseHolder>(R.layout.item_layout_team, allMyList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemFuWuZhan item = allMyList.get(position);
                    ImageView ivHead = helper.getView(R.id.ivHead);
                    TextView tvName = helper.getView(R.id.tvName);
                    TextView tvCount = helper.getView(R.id.tvCount);
                    ImageLoader.getInstance().loadHead(activity, item.logo, ivHead);
                    tvName.setText(item.name);
                    List<UserInfo> userList = item.userList;
                    if (userList == null || userList.isEmpty()) {
                        tvCount.setText("");
                    } else {
                        tvCount.setText(String.valueOf(userList.size()));
                    }
                }
            };
            myRecycler.setAdapter(myAdapter);
        }
        myAdapter.notifyDataSetChanged();
    }

    private void initJoinAdapter(List<ItemFuWuZhan> data) {
        allJoinList.clear();
        if (data != null) {
            allJoinList.addAll(data);
        }
        if (allJoinList.isEmpty()) {
            tvJoin.setText("");
        } else {
            tvJoin.setText(String.valueOf(allJoinList.size()));
        }
        if (joinAdapter == null) {
            joinAdapter = new BaseAdapter<ItemFuWuZhan, BaseHolder>(R.layout.item_layout_team, allJoinList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemFuWuZhan item = allJoinList.get(position);
                    ImageView ivHead = helper.getView(R.id.ivHead);
                    TextView tvName = helper.getView(R.id.tvName);
                    TextView tvCount = helper.getView(R.id.tvCount);
                    ImageLoader.getInstance().loadHead(activity, item.logo, ivHead);
                    tvName.setText(item.name);
                    List<UserInfo> userList = item.userList;
                    if (userList == null || userList.isEmpty()) {
                        tvCount.setText("");
                    } else {
                        tvCount.setText(String.valueOf(userList.size()));
                    }
                }
            };
            joinRecycler.setAdapter(joinAdapter);
        }
        joinAdapter.notifyDataSetChanged();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 996) {
                loadMineListData();
            }
        }
    }
}
